/*
 * Copyright (C) 2014 Tolstykh Vladislav
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package lib.search.engine;

import com.google.common.collect.TreeMultimap;
import java.util.TreeMap;
import lib.scripture.Chapter;
import lib.scripture.Verse;
import lib.sort.Sort;
import lib.sort.Sortable;

/**
 * Собственный алгоритм поиска подстроки.
 * @author Толстых Владислав
 */
public class Scanner {
    private Chapter CHAPTER;
    private Verse VERSE;
    /**
     * Острый резкий звук.
     */
    boolean bing;
    private TreeMultimap<Chapter,Verse> result;
    
    /**
     * "Искать иголку в стоге сена".
     * @param nextChapt глава для сохраниения, в случае успеха
     * @param nextVerse стих для сохранения, в случае успеха
     */
    public Scanner(Chapter nextChapt, Verse nextVerse) {
        result=TreeMultimap.create(new Sortable(Sort.CHAPTER), new Sortable(Sort.VERSE));
        CHAPTER=nextChapt;
        VERSE=nextVerse;        
    }
    
    /**
     * Описание алгоритма.
     * 1. Сканирование слева направо, сравнение справа налево
     * 2. Шаг сканера - длина образа
     * @param tm тримэп стиха
     * @param n иголка
     */
    public void left2Right(TreeMap<Integer, Character> tm, Needle n) {
        Pattern pattern = new Pattern(tm);
        
        if(!n.isRegex()){
            pattern.wholeLine(n.map());
        }else{
            pattern.overlap(n.map());
        }
        
        if(pattern.verified())
            result.put(CHAPTER,VERSE);
    }
    
    /**
     * 
     * @return 
     */
    public TreeMultimap<Chapter,Verse> result() {
        if(result.size()!=0) return result;
        return null;
    }
}
